package Classes;

import Forms.*;
import java.io.File;
import java.rmi.*;
import java.rmi.server.*;
import java.util.ArrayList;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author mau
 */
public class Server extends UnicastRemoteObject implements HInterface {
    Database db;
    ArrayList<User> usersList;
    DateTime dt;
    ServerFrame sf;
    int clients;
    int users;
    
    ArrayList<User> userEdit;
 
    
    ArrayList<Object> list;
    
    ArrayList<Object> searchList;
  
    public Server(ServerFrame a) throws RemoteException {
        sf = a;
        db = new Database(sf);
        dt = new DateTime();
        clients = sf.getNum("clients");
        users = sf.getNum("users");
    }
        
 
    @Override
    public String getCurrentTime() throws RemoteException {
        return dt.getCurrentTime();
    }
    
    @Override
    public String getCurrentDate() throws RemoteException {
        return dt.getCurrentDate();
    }
    
    @Override
    public String getDBDate() throws RemoteException {
        return dt.getDBDate();
    }
    
    @Override
    public String getCurrDate() throws RemoteException {
        return dt.getCurrDate();
    }
    
    @Override
    public String getCurrYear() throws RemoteException {
        return dt.getCurrYear();
    }
    
   @Override
    public boolean login(String username, String password, boolean status) throws RemoteException {
        boolean loggedin = false;
        
        try {
            usersList = (ArrayList<User>) getList("getlist-users").clone();
            for(User u : usersList) {
                if(username.equals(u.getUsername()) && password.equals(u.getPassword()) && !status) {
                    loggedin = true;
                    db.changeUserStatus(username, "In");
                    users += 1;
                    sf.appendTextArea(username + " has logged in.");
                    sf.updateLabel("users", users);
                } else if(username.equals(u.getUsername()) && password.equals(u.getPassword())) {
                    loggedin = true;
                }
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
        return loggedin;
    }
    
     @Override
    public ArrayList<Object> getList(String table)throws Exception {
        list = (ArrayList<Object>)db.getList(table).clone();
        return list;
    }    
    
   
    @Override
    public boolean checkUserStatus(String username) throws RemoteException {
        try {
            boolean in = db.checkStatus(username);
            if(in) {
                return true;
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
        return false;
    }

   
  
    
    @Override
    public void clientActivity(String username, String activity) {
        if(activity.equals("connect")) {
            clients += 1;
            sf.appendTextArea(username + " : A client has connected to the server.");
            sf.updateLabel("clients", clients);
        }
        
        if(activity.equals("disconnect")) {
            clients -= 1;
            sf.appendTextArea(username + " : A client has disconnected from the server.");
            sf.updateLabel("clients", clients);
        }
    }
    

    
 

    @Override
    public String getRealYear() throws Exception {
        return dt.getRealYear();
    }

    @Override
    public String getPrevYear() throws Exception {
        return dt.getPrevYear();
    }


    @Override
    public String getYear() throws RemoteException {
        return dt.getYear();
    }

    @Override
    public int getToday() throws Exception {
        return dt.getToday();
    }

    @Override
    public int getPast() throws Exception {
        return dt.getPast();
    }

    @Override
    public int getNext() throws Exception {
        return dt.getNext();
    }

 

}
